// +- OmniWorks Replacement History -
// tel_otac`campaignWeb10`WEB-INF`classes`com`axalto`simergy`web`campaign`common`utility:NumberUtil.java;1
// 1*[405427] 24-MAY-2006 09:10:46 (GMT) Zhihao
// "All common methods for number operation"
// +- OmniWorks Replacement History -
// tel_otac`campaignWeb10`WEB-INF`classes`com`axalto`simergy`web`campaign`common`utility:NumberUtil.java;1
package org.ihelpuoo.lang;

/**
 * <b>NumberUtil class:</b>
 * <hr color=grey size=1>
 *
 * All common methods for number operation
 *
 * @author zozoh  created @ 2006
 *
 */
public class NumericUtils
{

	/**
	 * Get a double by some precision
	 * 
	 * @param n: the number
	 * @param r: the precision, >0
	 * @return
	 */
	public static double round(double n, int r)
	{
		// TODO ZZH: when the r is big, double will out of rang...
		if (r < 0)
			return n;
		if (r == 0)
			return (int)(n + (float).5);
		double ele = Math.pow(10,r);
		double value = n * ele + (float).5;
		value = (int)value;
		value = value / ele;
		return value;
	}

	public static int parseInt(String s, int def)
	{
		try
		{
			return Integer.parseInt(s);
		}
		catch (NumberFormatException e)
		{
			return def;
		}
	}

	public static int parseInt(String s, int radix, int def)
	{
		try
		{
			return Integer.parseInt(s,radix);
		}
		catch (NumberFormatException e)
		{
			return def;
		}
	}
}
